package com.hnkywl.sts.service.organ;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.ZyfDao;
import com.hnkywl.sts.entity.organ.Zyf;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Melody on 14-6-24.
 */
@Service
public class ZyfService {
    private BmService bmService;
    private ZyfDao zyfDao;

    @Transactional
    public String findAllZyf(String qc, String jc, int pageNo, int pageSize) {
        Page<Zyf> zyfPage = this.zyfDao.findZyfList(qc, jc, pageNo, pageSize);
        List<Zyf> zyfList = zyfPage.getResult() == null ? new ArrayList<Zyf>() : zyfPage.getResult();
        String zyfJson = BaseUtils.fetchCollectionToJson(zyfList, "id", "qc", "jc", "bm", "zjm", "bz", "sfzx", "zyl", "lxdh", "lxr");
        return "{\"total\":" + zyfPage.getTotalCount() + ",\"rows\":[" + zyfJson + "]}";
    }

    @Transactional
    public Zyf findZyfDetail(Long id) {
        return this.zyfDao.get(id);
    }

    @Transactional
    public void saveZyf(Zyf zyf) throws RuntimeException {
        if (zyfDao.checkUnique(zyf.getId(), zyf.getQc(), "qc")) {
            throw new RuntimeException("此全称已存在");
        }
        if (zyfDao.checkUnique(zyf.getId(), zyf.getJc(), "jc")) {
            throw new RuntimeException("简称已存在");
        }
        if (zyfDao.checkUnique(zyf.getId(), zyf.getBm(), "bm")) {
            throw new RuntimeException("编码重复");
        }
        if (StringUtils.isEmpty(zyf.getBm())) {
            String bm = this.getJgbm();
            zyf.setBm(bm);
        }
        if (StringUtils.isEmpty(zyf.getZt())) {
            zyf.setZt("ybc");
            zyf.setSfzx(!zyf.getSfzx());
        }
        zyf.setDateCreated(new Date());
        zyf.setLastUpdated(new Date());
        this.zyfDao.save(zyf);
    }

    @Transactional
    public void updateZyf(Zyf zyf) throws RuntimeException {
        Zyf temp = this.zyfDao.get(zyf.getId());
        zyf.setSfzx(!zyf.getSfzx());
        temp = (Zyf) ReflectionUtils.getInstance(temp, zyf);
        temp.setSfzx(false);
        temp.setLastUpdated(new Date());
        this.zyfDao.save(temp);
    }

    @Transactional
    public void deleteZyf(Long zyfId) {
        Zyf zyf = zyfDao.get(zyfId);
        if (!zyf.getSfzx()) {
            throw new RuntimeException("禁止删除！质押方已启用");
        }
        zyf.setLastUpdated(new Date());
        zyf.setDeleted(1);
        this.zyfDao.save(zyf);
    }

    @Transactional
    public void startZyf(Long id) {
        Zyf zyf = zyfDao.get(id);
        zyf.setLastUpdated(new Date());
        zyf.setSfzx(false);
        this.zyfDao.save(zyf);
    }

    @Transactional
    public void stopZyf(Long id) {
        Zyf zyf = zyfDao.get(id);
        zyf.setLastUpdated(new Date());
        zyf.setSfzx(true);
        this.zyfDao.save(zyf);
    }
    @Transactional
    public String findZyfJc(String jc, int pageNo, int pageSize) {
        List<Map> allPage = this.zyfDao.findZyfJcList(jc, pageNo, pageSize);
        Gson gson = new Gson();
        String result = gson.toJson(allPage);
        return result;
    }
    public boolean ajaxValidate(Long id, String value, String field) {
        return zyfDao.checkUnique(id, value, field);
    }
    
    public String getJgbm() {
        return bmService.getJgbm();
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
    
    @Autowired
    public void setZyfDao(ZyfDao zyfDao) {
        this.zyfDao = zyfDao;
    }
}
